package app.netant.bt.btcat;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
/**
 * 详细页面分析类
 * 18禁ゲーム
 * @author JiaJ
 * @date 2018年6月27日
 *
 */
public class BtCatNetDetail extends NetAntBase {
	
	private BtCatEntity entity;

	public BtCatNetDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(BtCatEntity entity) {
		this.entity = entity;
	}

	/**
	 * 文件标题分析
	 * @return
	 */
	private void analyseFileName(){		
		Elements activePart = document.select("#wrapper #content #wall h2");
		if(activePart.size()>0){	
			entity.setResName(this.getString(activePart.get(0).text()));
		}	
	}
	
	private void analyseName(){
		Elements activePart = document.select("#wrapper #content #wall h2");
		if(activePart.size()>0){
			entity.setResNameSim(cutString(this.getString(activePart.get(0).text()), '[', ']'));
		}
	}
	
	private void analyseGameDate(){
		Elements activePart = document.select("#wrapper #content #wall h2");
		if(activePart.size()>0){
			entity.setResDate(this.getDateString(this.getString(activePart.get(0).text()), '[', ']'));
		}
	}
	
	private void analyseTags(){
		Elements tagsParts = document.select("#wrapper #content #wall .detail-table.detail-width tbody tr th");
		Elements valsParts = document.select("#wrapper #content #wall .detail-table.detail-width tbody tr td");
		
		for(int i=0;i<tagsParts.size();i++){
			String name = tagsParts.get(i).text();
			String value = valsParts.get(i).text();
			entity.setTags(name, value);
		}
	}
	
	private void analyseLink(){
		
		Elements elementParts = document.select("#wrapper #content #wall .detail-panel.detail-width .detail-panel.detail-width");
		for(int i=0;i<elementParts.size();i++){
			
			String tagName = elementParts.get(i).select(".panel-header").text();
			String tagValue = elementParts.get(i).select(".panel-body").text();
			
			if("种子下载".equals(tagName)){
				tagValue = "https://btcat.org"+elementParts.get(i).select(".panel-body a").attr("href");
			}
			else if("磁力链接".equals(tagName) || "迅雷下载".equals(tagName)){
				tagValue = elementParts.get(i).select(".panel-body a").text();
			}
			
			else if("相关链接".equals(tagName)){
				
				Elements tabParts = elementParts.get(i).select(".panel-body a");
				tagValue = "";
				for(int j=0;j<tabParts.size();j++){
					tagValue += tabParts.get(j).text()+",";
				}
				
			}
			
			else if("文件列表".equals(tagName)){
				
				Elements fileParts = elementParts.get(i).select(".panel-body ol li");
				tagValue = "";
				for(int j=0;j<fileParts.size();j++){
					
					String value = fileParts.get(j).text();
					if(value.indexOf("如果您看到此文件，请升级到BitComet(比特彗星)")>0){
						continue;
					}
					if(value.indexOf("please update to BitComet 0.85")>0){
						continue;
					}
					
					tagValue += value+"\n";
				}
				
				if(tagValue.length()>1300){
					tagValue = tagValue.substring(0,1300)+"...";
				}
				
			}
			
			else{
				tagValue = elementParts.get(i).select(".panel-body a").text();
			}
			
			entity.setTags(tagName,tagValue);
		}
		
		
	}
	
	public void analyse(){
		
		this.analyseName();
		this.analyseTags();
		this.analyseLink();
		this.analyseFileName();
		this.analyseGameDate();
		
	}
	
	public static void main(String[] args) {
		BtCatNetDetail detail = new BtCatNetDetail("https://btcat.org/btinfo-f2e85629e4db4e23fa9bc958a175c88ebe421322.html");
		//detail.analyseFileName();
		//detail.analyseName();
		detail.analyseLink();
	}
	
}
